package flc.ast.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ImageUtils;

import flc.ast.R;

public class StkAudioTrackView extends BaseAvTrackView {

    protected Bitmap mAudioWaveBmp;

    public StkAudioTrackView(Context context) {
        this(context, null);
    }

    public StkAudioTrackView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StkAudioTrackView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initAttrs(AttributeSet attrs) {
        super.initAttrs(attrs);

        if (attrs == null) {
            return;
        }
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.StkAudioTrackView);
        Drawable drawable = array.getDrawable(R.styleable.StkAudioTrackView_av_audio_wave_bmp);
        if (drawable != null) {
            mAudioWaveBmp = ImageUtils.drawable2Bitmap(drawable);
        }
        if (mAudioWaveBmp == null) {
            mAudioWaveBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ayinpp2);
        }
        array.recycle();
    }

    @Override
    protected void drawTrackContent(Canvas canvas) {
        if (mAudioWaveBmp == null) {
            return;
        }
        RectF rectF = new RectF(mLeftShadowRectF);
        rectF.left = mLeftSlideBarMinLeft;
        rectF.right = mRightSlideBarMaxRight;
        canvas.drawBitmap(mAudioWaveBmp, null, rectF, null);
    }

    /**
     * 设置音频波形位图
     *
     * @param mAudioWaveBmp
     */
    public void setAudioWaveBmp(Bitmap mAudioWaveBmp) {
        this.mAudioWaveBmp = mAudioWaveBmp;
        post(new Runnable() {
            @Override
            public void run() {
                invalidate();
            }
        });
    }
}

